package com.yx.shop.web.controller;

import com.yx.shop.common.Const;
import com.yx.shop.entitly.User;
import com.yx.shop.service.UserService;
import com.yx.shop.service.impl.UserServiceImpl;
import com.yx.shop.utils.Base64Utils;
import com.yx.shop.utils.MD5Utils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Classname ${NAME}
 * @Description TODO:
 * @Date 2021/05/06 16:39
 * @Created by yx
 */
@WebServlet(name = "UserController",value = "/user")
public class UserController extends BaseServlet {

    private UserService userService = new UserServiceImpl();

    public String checkName(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        User user = userService.getUserByName(username);

        if (user == null){
            //数据库中没有此人,可以注册
            return Const.NAME_IS_NOT_EXSIT;
        }else {
            return Const.NAME_IS_EXSIT;
        }
    }

    /**
     * @Author:yx
     * @return java.lang.String
     * @Date 2021/05/06 17:13
     * @param request
     * @param response
     * @Description:
     **/
    public String register(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String sex = request.getParameter("gender");

        boolean flag = userService.register(username,password,email,sex);

        if (flag){
            //注册成功跳转页面
            return Const.REDIRECT+"/registerSuccess.jsp";
        }else {
            //保存失败信息到session当中,回到注册页面
            request.getSession().setAttribute("registerMsg", "注册失败,请稍后再尝试");
            return Const.REDIRECT+"/redister.jsp";
        }
    }


    public String activate(HttpServletRequest request,HttpServletResponse response){
        //1 获取参数
        String e = request.getParameter("e");
        String c = request.getParameter("c");
        //2 业务层处理激活
        //0 激活失败 1激活成功 2 已激活
        int result = userService.activate(e,c);
        //3 结果
        if (result == 2) {
            request.setAttribute("msg", "已激活,不需要重复激活");
        } else if (result == 1) {
            request.setAttribute("msg", "激活成功,返回主页");
        } else {
            request.setAttribute("msg", "激活失败,请稍后重试");
        }
        return Const.FORWORD+"/message.jsp";
    }

    public void checkCode(HttpServletRequest request ,HttpServletResponse response) throws IOException {
        String code = request.getParameter("code");
        System.out.println(code);
        String codes = (String) request.getSession().getAttribute(Const.CODES);
        System.out.println(codes);
        if(code!=null&&code.equalsIgnoreCase(codes)){
            response.getWriter().print(0);
        }
    }


    public String login(HttpServletRequest request,HttpServletResponse response){
        //1.获取账号密码验证码
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String code = request.getParameter("vcode");
        String auto = request.getParameter("auto");


        //2.对比验证码 --->错误--->重定向至登录页面
        String codes = (String) request.getSession().getAttribute(Const.CODES);
        if(code==null||!code.equalsIgnoreCase(codes)){
            request.setAttribute("msg","验证码错误");
            return Const.FORWORD+"/login.jsp";
        }
        //
        /*3.对比账号密码
        * 失败--->重定向登录页面
        * 成功--->激活状态  --->首页 用户信息存入session
        *        >未激活 ---->提示去激活
        * */

        UserService userService = new UserServiceImpl();
        User user = userService.login(username, password);

        if (user==null){
            request.setAttribute("msg","账号或密码错误");
            return Const.FORWORD+"/login.jsp";
        }
        if (user.getUstatus()==Const.UserInfo.STATUS_NOT_ACT){
            //账户未激活
            request.setAttribute("msg","账号未激活");
            return Const.FORWORD+"/login.jsp";
        }

        //登录成功重定向至首页
        request.getSession().setAttribute("loginUser",user);

        //判断是否自动登录
        if (auto!=null){
            //勾选,设置cookie
            String content = username+":"+password;
            //加密
//            content = MD5Utils.md5(content);
            content = Base64Utils.encode(content);
            Cookie cookie = new Cookie(Const.AUTO_NAME,content);
            cookie.setPath("/");
            cookie.setMaxAge(14*24*60*60);
            response.addCookie(cookie);
        }
        return Const.REDIRECT+"/index.jsp";
    }

    public String loginOut(HttpServletRequest request,HttpServletResponse response){
       //1.清空自动登录session(推荐)
        request.getSession().removeAttribute("loginUser");
       /* //清空所有session(不推荐,可能含有其它重要的session)
        request.getSession().invalidate();*/
        //2.清空cookie存储的登录信息--->覆盖
        Cookie cookie = new Cookie(Const.AUTO_NAME,"");
        cookie.setPath("/");
        //删除cookie
        cookie.setMaxAge(0);
        response.addCookie(cookie);
       /* //3.转发登录页面
        request.setAttribute("msg","注销登录成功");
        return Const.FORWORD+"/login.jsp";*/
       //3.重定向至登录界面
        return Const.REDIRECT+"/login.jsp";
    }
}
